/**
 * DICOM URL处理工具测试脚本
 * 用于验证URL转换功能是否正常工作
 */
import {
	convertToProxyUrl,
	processDicomUrl,
	processDicomUrls,
	needsProxyConversion
} from "./dicomUrlHandler";

// 测试用例
const testCases = [
	{
		input: "http://10.168.128.102:7001/20250924/CT/25528618_2.25.67089079276993669930313170171575576375/1.2.156.112605.66988329203163.250924022144.4.12348.13952.dcm",
		expected:
			"/files/20250924/CT/25528618_2.25.67089079276993669930313170171575576375/1.2.156.112605.66988329203163.250924022144.4.12348.13952.dcm",
		description: "完整内网URL转换为代理路径"
	},
	{
		input: "/files/20250924/CT/25528618_2.25.67089079276993669930313170171575576375/1.2.156.112605.66988329203163.250924022144.4.12348.13952.dcm",
		expected:
			"/files/20250924/CT/25528618_2.25.67089079276993669930313170171575576375/1.2.156.112605.66988329203163.250924022144.4.12348.13952.dcm",
		description: "已经是代理路径，保持不变"
	},
	{
		input: "dicomweb:/files/20250924/CT/25528618_2.25.67089079276993669930313170171575576375/1.2.156.112605.66988329203163.250924022144.4.12348.13952.dcm",
		expected:
			"dicomweb:/files/20250924/CT/25528618_2.25.67089079276993669930313170171575576375/1.2.156.112605.66988329203163.250924022144.4.12348.13952.dcm",
		description: "已经是dicomweb格式，保持不变"
	},
	{
		input: "http://example.com/somefile.dcm",
		expected: "http://example.com/somefile.dcm",
		description: "外部URL，保持不变"
	}
];

// 运行测试
function runTests() {
	console.log("=== DICOM URL处理工具测试 ===\n");

	let passed = 0;
	let failed = 0;

	testCases.forEach((testCase, index) => {
		const result = convertToProxyUrl(testCase.input);
		const isPass = result === testCase.expected;

		console.log(`测试 ${index + 1}: ${testCase.description}`);
		console.log(`输入: ${testCase.input}`);
		console.log(`期望: ${testCase.expected}`);
		console.log(`实际: ${result}`);
		console.log(`结果: ${isPass ? "✓ 通过" : "✗ 失败"}`);
		console.log("---");

		if (isPass) {
			passed++;
		} else {
			failed++;
		}
	});

	console.log(`\n测试结果: ${passed} 通过, ${failed} 失败`);

	// 测试processDicomUrl函数
	console.log("\n=== processDicomUrl函数测试 ===");
	const testUrl = "http://10.168.128.102:7001/test.dcm";
	const processed = processDicomUrl(testUrl);
	console.log(`输入: ${testUrl}`);
	console.log(`输出: ${processed}`);
	console.log(
		`是否包含dicomweb前缀: ${processed.startsWith("dicomweb:") ? "✓ 是" : "✗ 否"}`
	);

	// 测试批量处理
	console.log("\n=== processDicomUrls批量处理测试 ===");
	const urls = [
		"http://10.168.128.102:7001/file1.dcm",
		"http://10.168.128.102:7001/file2.dcm",
		"/files/existing.dcm"
	];
	const processedUrls = processDicomUrls(urls);
	console.log("输入URL列表:", urls);
	console.log("处理后的URL列表:", processedUrls);

	// 测试代理转换检查
	console.log("\n=== needsProxyConversion代理转换检查测试 ===");
	const testUrl1 = "http://10.168.128.102:7001/test.dcm";
	const testUrl2 = "http://example.com/test.dcm";
	console.log(
		`URL "${testUrl1}" 需要代理转换: ${
			needsProxyConversion(testUrl1) ? "✓ 是" : "✗ 否"
		}`
	);
	console.log(
		`URL "${testUrl2}" 需要代理转换: ${
			needsProxyConversion(testUrl2) ? "✓ 是" : "✗ 否"
		}`
	);
}

// 如果直接运行此文件，则执行测试
if (import.meta.url === `file://${process.argv[1]}`) {
	runTests();
}

export default runTests;
